var t = getApp();

Page({
    data: {
        goods: [],
        radio: "",
        zIndex: 999,
        show: !1,
        swiperName: {
            loop: !0,
            direction: "vertical",
            slidesPerView: 5,
            freeMode: !0,
            freeModeMomentum: !0,
            freeModeMomentumVelocityRatio: 1,
            autoplay: 1e3
        },
        prizeIndex: 999,
        arrNum: [ 0, 1, 2, 3, 7, 11, 15, 14, 13, 12, 8, 4 ],
        clickFlage: !0,
        prizeInfoShow: !1,
        noPrizeShow: !1,
        havePrizeShow: !1,
        haveLottery: !1,
        timer1: "",
        timer2: "",
        s1: "",
        s2: -1,
        lotteryId: "",
        activityModel: {},
        hasRecord: !1,
        remainingTimes: 0,
        prizeList: [],
        prizeLevel: "",
        prizeName: "",
        prizeUrl: "",
        prizers: [],
        timeFlag: 0,
        startStatus: 1,
        prizerListBol: !1,
        lotteryInfo: {},
        prizeInfo: {},
        love: 0,
        amount_word: "",
        love_word: "",
        point_word: "",
        toolbar: !1,
        empty_prize_prompt: "",
        share_id: ""
    },
    onLoad: function(a) {
        a.mid && t._setMid(a.mid), a.share_id && this.setData({
            share_id: a.share_id
        }), console.log(this.data.share_id), a.lotteryId && this.setData({
            lotteryId: a.lotteryId
        }), this.getData();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareTimeline: function() {
        var t = wx.getStorageSync("yz_uid"), a = "";
        return t && (a = t), {
            query: "lotteryId=" + this.data.lotteryId + "&mid=" + a
        };
    },
    onShareAppMessage: function(t) {
        var a = wx.getStorageSync("yz_uid"), e = "";
        return a && (e = a), {
            path: "/packageC/orders/Lottery/Lottery?lotteryId=" + this.data.lotteryId + "&mid=" + e + "&share_id=" + e
        };
    },
    confirmGoods: function() {
        this.setData({
            show: !1
        }), this.data.radio && wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + this.data.radio
        });
    },
    getData: function() {
        var a = this, e = t.getNetAddresss("plugin.lucky-draw.frontend.draw.index");
        this.data.share_id && (e += "&share_id=" + this.data.share_id), t._getNetWork({
            url: e,
            data: {
                lotteryId: this.data.lotteryId
            },
            success: function(t) {
                var e = t.data;
                if (1 == e.result) {
                    a.setData({
                        shop_logo: e.data.shop_logo,
                        amount_word: e.data.amount_word,
                        love_word: e.data.love_word,
                        point_word: e.data.point_word,
                        credit1: e.data.point.credit1,
                        success_mini_url: e.data.activityModel.success_mini_url,
                        goods: e.data.goods,
                        activityModel: e.data.activityModel,
                        remainingTimes: e.data.surplus_time
                    }), e.data.love && a.setData({
                        love: e.data.love[0]
                    }), e.data.goods && e.data.goods.length > 0 && a.setData({
                        radio: e.data.goods[0].id
                    }), e.data.result_arr.forEach(function(t) {
                        t.bgSrc = "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/lottery/item_bg.png";
                    }), e.data.result_arr.splice(5, 0, " "), e.data.result_arr.splice(6, 0, " "), e.data.result_arr.splice(9, 0, " "), 
                    e.data.result_arr.splice(10, 0, " "), a.setData({
                        prizeList: e.data.result_arr,
                        prizers: e.data.list
                    });
                    var i = a.data.prizeList[4];
                    a.setData({
                        "prizeList[4]": a.data.prizeList[7],
                        "prizeList[7]": i
                    }), i = a.data.prizeList[12], a.setData({
                        "prizeList[12]": a.data.prizeList[13],
                        "prizeList[13]": i
                    }), i = a.data.prizeList[14], a.setData({
                        "prizeList[14]": a.data.prizeList[15],
                        "prizeList[15]": i
                    }), a.statusFun(Date.parse(new Date()) / 1e3, a.data.activityModel.countdown_time[0], a.data.activityModel.countdown_time[1]), 
                    a.data.prizers && a.data.prizers.length > 5 && a.setData({
                        prizerListBol: !0
                    });
                }
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    statusFun: function(t, a, e) {
        t < a ? this.setData({
            startStatus: 0
        }) : t > a && t < e ? this.setData({
            startStatus: 1
        }) : t > e && this.setData({
            startStatus: 2
        });
    },
    prizeZhuan: function() {
        var a = this;
        if (this.setData({
            prizeIndex: 0
        }), 0 == this.data.activityModel.draw_type) return this.luckdraw(), !1;
        var e = "use_point", i = "point_word", r = "credit1";
        2 == this.data.activityModel.draw_type && (e = "use_love", i = "love_word", r = "love");
        var o = Number(this.data.activityModel[e]), s = Number(this.data[r]), d = "抽奖需要消耗".concat(o).concat(this.data[i], "，是否继续？");
        wx.showModal({
            title: "提示",
            content: d,
            success: function(e) {
                if (e.confirm) if (o > s) t.tips(a.data[i] + "不足"); else {
                    a.luckdraw();
                    var d = {};
                    d[r] = (s - o).toFixed(2), a.setData(d);
                }
            }
        });
    },
    luckdraw: function() {
        var a = this, e = this;
        if (this.data.remainingTimes > 0) {
            if (this.data.clickFlage) if (1 === this.data.startStatus) {
                this.setData({
                    clickFlage: !1,
                    timer1: setInterval(function() {
                        a.move();
                    }, 100)
                });
                var i = t.getNetAddresss("plugin.lucky-draw.frontend.draw.doDraw");
                t._getNetWork({
                    url: i,
                    data: {
                        lotteryId: this.data.lotteryId
                    },
                    success: function(t) {
                        var a = t.data;
                        1 == a.result ? (e.setData({
                            remainingTimes: e.data.remainingTimes - 1
                        }), a.data.result ? (e.setData({
                            hasRecord: !0,
                            prozeLevel: a.data.result.item,
                            prizeName: a.data.result.prize,
                            prizeUrl: a.data.result.thumb_url
                        }), e.getData()) : (e.getData(), e.setData({
                            hasRecord: !1,
                            prozeLevel: a.data.item,
                            prizeName: a.data.prize,
                            prizeUrl: a.data.thumb_url,
                            empty_prize_prompt: a.data.empty_prize_prompt
                        })), setTimeout(function() {
                            clearInterval(e.data.timer1), e.lowSpeed();
                        }, 1200)) : (wx.showModal({
                            title: "提示",
                            content: a.msg,
                            showCancel: !1,
                            success: function(t) {
                                t.confirm && e.data.goods && e.data.goods.length > 0 && e.setData({
                                    show: !0
                                });
                            }
                        }), e.setData({
                            clickFlage: !0
                        }), clearInterval(e.data.timer1), clearInterval(e.data.timer2));
                    },
                    fail: function(t) {
                        console.log(t);
                    }
                });
            } else 0 === this.data.startStatus ? wx.showToast({
                icon: "none",
                title: "活动尚未开始",
                duration: 1500
            }) : 2 === this.data.startStatus && wx.showToast({
                icon: "none",
                title: "活动已经结束",
                duration: 1500
            });
        } else this.setData({
            prizeInfoShow: !0,
            haveLottery: !0
        });
    },
    lowSpeed: function() {
        var t = this;
        clearInterval(this.data.timer1), this.setData({
            clickFlage: !1,
            timer2: setInterval(function() {
                t.move();
            }, 300)
        }), setTimeout(function() {
            t.setData({
                s2: t.data.prozeLevel
            });
        }, 900);
    },
    move: function() {
        0 === this.data.prizeIndex ? this.setData({
            prizeIndex: this.data.prizeIndex + 1
        }) : 12 === this.data.prizeIndex ? this.setData({
            prizeIndex: 0
        }) : this.setData({
            prizeIndex: this.data.prizeIndex + 1
        }), -1 !== this.data.s2 && this.data.prizeList[this.data.arrNum[this.data.prizeIndex]] && this.data.prizeList[this.data.arrNum[this.data.prizeIndex]].item == this.data.s2 && (clearInterval(this.data.timer1), 
        clearInterval(this.data.timer2), this.stop()), this.setData({
            timeFlag: this.data.timeFlag + 100
        }), this.data.timeFlag >= 1e4 && !this.data.prizeName && (clearInterval(this.data.timer1), 
        clearInterval(this.data.timer2), wx.showToast({
            icon: "none",
            title: "当前抽奖人数过多，稍后再来",
            duration: 1500
        }), this.setData({
            clickFlage: !0
        }));
    },
    stop: function() {
        this.setData({
            s2: -1,
            clickFlage: !1
        }), this.prizeInfo();
    },
    prizeInfo: function() {
        var t = this;
        setTimeout(function() {
            t.setData({
                clickFlage: !0
            }), t.data.hasRecord ? t.setData({
                prizeInfoShow: !0,
                havePrizeShow: !0
            }) : t.setData({
                prizeInfoShow: !0,
                noPrizeShow: !0
            });
        }, 800);
    },
    lotteryRecord: function() {
        wx.navigateTo({
            url: "/packageC/orders/LotteryRecord/LotteryRecord?lotteryId=" + this.data.lotteryId
        });
    },
    onChange: function(t) {
        this.setData({
            radio: t.detail
        });
    },
    closePop: function() {
        this.setData({
            show: !1
        });
    },
    close: function(t) {
        var a = t.currentTarget.dataset.name;
        this.setData({
            prizeInfoShow: !1,
            noPrizeShow: !1,
            haveLottery: !1,
            havePrizeShow: !1
        }), "success" == a && this.data.success_mini_url && wx.navigateTo({
            url: this.data.success_mini_url
        });
    },
    goIndex: function() {
        this.setData({
            prizeInfoShow: !1,
            noPrizeShow: !1
        }), wx.reLaunch({
            url: "/packageG/index/index"
        });
    },
    showToolbar: function() {
        this.setData({
            toolbar: !this.data.toolbar
        });
    },
    gotoHome: function() {
        wx.reLaunch({
            url: "/packageG/index/index"
        });
    },
    gotoMember: function() {
        wx.reLaunch({
            url: "/packageG/member_v2/member_v2"
        });
    }
});